package com.zx.mobile.service.proxy.share;

import com.zx.map.service.MessageSendLogService;
import com.zx.map.service.model.MerchantInfoModel;
import com.zx.mobile.MobileCommonHelper;
import com.zx.mobile.domain.shareregister.ShareRegisterRequest;
import com.zx.platform.utils.MD5Util;
import com.zx.platform.utils.PmpUtils;
import org.apache.commons.lang3.StringUtils;

import java.io.UnsupportedEncodingException;
import java.util.Date;

public class ShareRegisterCommonProxy {

	/**
	 * app注册合法性校验
	 * 
	 * @param shareRegisterRequest
	 * @param messageSendLogService 
	 * @return
	 * 
	 */
	public static String checkShareRegister(
			final ShareRegisterRequest shareRegisterRequest, MessageSendLogService messageSendLogService) {
		
		if (StringUtils.isNotBlank(shareRegisterRequest.getTelephone())) {
			if (!PmpUtils.isMobileNo(shareRegisterRequest.getTelephone())) {
				return "手机号不合法";
			}
		} else {
			return "输入项必填";
		}
		if (StringUtils.isBlank(shareRegisterRequest.getLoginPassword())) {
			return "输入项必填";
		}
		if (StringUtils.isBlank(shareRegisterRequest.getInvitationCode())) {
			return "输入项必填";
		}
		if (StringUtils.isBlank(shareRegisterRequest.getConfirmPassword())) {
			return "输入项必填";
		}
		if (StringUtils.isNotBlank(shareRegisterRequest.getVerifyCode())) {
			if (!messageSendLogService.checkVerifyCode(shareRegisterRequest.getTelephone(),shareRegisterRequest.getVerifyCode(),MobileCommonHelper.MESSAGETYPE_REGISTER)) {
				return "验证码不正确";
			}
		} else {
			return "输入项必填";
		}

		return null;
	}	
	

	/**
	 * app注册请求转换商户信息
	 * 
	 * @param shareRegisterRequest
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	public static MerchantInfoModel transFormRequest(
			ShareRegisterRequest shareRegisterRequest)
			throws UnsupportedEncodingException {
		MerchantInfoModel merchantModel = new MerchantInfoModel();
		merchantModel.setCreateDate(new Date());
		merchantModel.setSysAccount(shareRegisterRequest.getTelephone());
		merchantModel.setTelephone(shareRegisterRequest.getTelephone());//由于app端注册均采取手机号 
		merchantModel.setSysPassword(PmpUtils.encoderByMD5(MD5Util
                .getMD5Str(shareRegisterRequest
				.getLoginPassword())));
		merchantModel.setMerchantType(MobileCommonHelper.MERCHANTTYPE_PERSON);//商户类型
		merchantModel.setProductType(MobileCommonHelper.PRODUCTTYPE_APP);//产品类型  手机端用户
		merchantModel.setStatus(MobileCommonHelper.APPUSERSTATUS_NOTREQUEST);//设置用户状态为待实名认证状态
		return merchantModel;
	}

}
